<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<!--选择入库记录-->
<div class="modal fade" id="modal-select">
    <div class="modal-dialog" style="width:1400px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">选择入库记录</h4>
            </div>
            <div class="modal-body">
                <table id="src-table"></table>
                <form id="storeForm" onsubmit="return false;">
                    <div class="row" style="margin-top: 20px">
                        <div class="form-inline">
                            <div class="form-group" style="margin-right: 10px;">
                                <label>使用周期</label>
                                <input type="text" name="odaProCode" class="form-control" placeholder=""
                                       style="width: 100px">
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>项目文本</label>
                                <input type="text" name="odaProName" class="form-control" placeholder=""
                                       style="width: 100px">
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>物料描述</label>
                                <input type="text" name="odaMatName" class="form-control" placeholder=""
                                       style="width: 100px">
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>计划跟踪号</label>
                                <input type="text" name="odaTrackCode" class="form-control" placeholder=""
                                       style="width: 100px">
                            </div>
                            <button type="button" class="btn btn-success" id="btn-search-store">
                                <span class="Bold">搜索</span>
                            </button>
                        </div>
                    </div>
                </form>
                <table id="store-table"></table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!--查看-->
<div class="modal fade" id="modal-info">
    <div class="modal-dialog" style="width:800px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">详细信息</h4>
            </div>
            <div class="modal-body">
                <div id="info">
                    <div class="col-md-12">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">SAP物料编码</label>
                                <div class="col-sm-9">
                                    <span id="odaSapCode"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">物料描述</label>
                                <div class="col-sm-9">
                                    <span id="odaMatName"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">数量</label>
                                <div class="col-sm-9">
                                    <span id="odaNum"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">单位</label>
                                <div class="col-sm-9">
                                    <span id="odaUnit"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">计划跟踪号</label>
                                <div class="col-sm-9">
                                    <span id="odaTrackCode"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">图号</label>
                                <div class="col-sm-9">
                                    <span id="odaDrawingCode"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">项目文本</label>
                                <div class="col-sm-9">
                                    <span id="odaProName"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">使用周期</label>
                                <div class="col-sm-9">
                                    <span id="odaProCode"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">库存地</label>
                                <div class="col-sm-9">
                                    <span id="whiName"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">仓位</label>
                                <div class="col-sm-9">
                                    <span id="odaPosition"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">记账日期</label>
                                <div class="col-sm-9">
                                    <span id="odaBookkeepingDate"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">特殊说明</label>
                                <div class="col-sm-9">
                                    <span id="odaSpec"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
<!--确认回货模态框-->
<div class="modal fade" id="modal-confirm-return">
    <div class="modal-dialog" style="width:1200px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">填写品牌</h4>
            </div>
            <div class="modal-body">
                <form id="confirmReturnForm" onsubmit="return false;">
                    <div class="row" style="margin-top: 20px">
                        <div class="form-inline">
                            <div class="form-group" style="margin-right: 10px;">
                                <label class="control-label">品牌</label>
                                <label class="control-label">
                                    <input type="text" name="brand" class="form-control" placeholder="请输入品牌"
                                           style="width: 100px">
                                </label>
                            </div>

                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info file-contract btn-file" id="btn-confirm-return-submit">确认
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<section class="content-header"><h1>外协件回货确认</h1></section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>计划员</label>
                                    <input type="text" name="sppPlanner" class="form-control" placeholder="请输入计划员">
                                </div>
                                <div class="form-group">
                                    <label>SAP物料编码</label>
                                    <input type="text" name="sppSapCode" class="form-control" placeholder="请输入SAP物料编码">
                                </div>
                                <div class="form-group">
                                    <label>物料描述</label>
                                    <input type="text" name="sppMatDesc" class="form-control" placeholder="请输入采购物料描述">
                                </div>
                                <div class="form-group">
                                    <label>项目编号</label>
                                    <input type="text" name="sppProjectCode" class="form-control" placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label>项目名称</label>
                                    <input type="text" name="sppProjectName" class="form-control" placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label>跟踪号</label>
                                    <input type="text" name="sppTrackCode" class="form-control" placeholder="请输入跟踪号">
                                </div>
                                <div class="form-group">
                                    <label>确认状态</label>
                                    <select name="sppRetStatus" class="form-control">
                                        <option value="">全部</option>
                                        <option value="0" selected="selected">未确认</option>
                                        <option value="1">已确认</option>
                                    </select>
                                </div>
<!--                                <div class="form-group">-->
<!--                                    <label>预入库状态</label>-->
<!--                                    <select name="sppPreStatus" class="form-control">-->
<!--                                        <option value="" selected="selected">全部</option>-->
<!--                                        <option value="0">未到货</option>-->
<!--                                        <option value="1">预入库</option>-->
<!--                                    </select>-->
<!--                                </div>-->
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <div id="toolbar-main" class="form-inline">
                        <!--                        <a shiro:hasPermission="plan:buyretconfirm:prestorage" href="javascript:void(0);" class="btn btn-info pull-right" onclick="openPreModal()">-->
                        <!--                            <span class="Bold">预入库</span>-->
                        <!--                        </a>-->
                        <button shiro:hasAllPermissions="plan:buyretconfirm:confirm" class="btn btn-success pull-right"
                                style="margin-right: 10px" onclick="batchOrderConfirmation()"><span
                                class="Bold">批量回货确认</span></button>
                    </div>
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var table, preTable, srcTable, storeTable, shoppingPlanId;


    /** 打开选择模态框 */
    function openSelectModal(row) {
        let data = [];
        data.push(row);
        shoppingPlanId = row.sppId;
        js.table.load(srcTable, data);
        js.table.search(storeTable);
        js.modal.open("modal-select");
    }

    /** 查看 */
    function openInfoModal(row) {
        $("#info").fillSpan(row);
        js.modal.open("modal-info");
    }


    /** 预入库模太框 */
    function openPreModal() {
        js.table.search(preTable);
        js.modal.open("modal-pre");
    }

    /** 确认回货 */
    function submitReturn(sppId, odaId) {

        js.post({
            url: ctx + "buy/outsourceReturn/checkProCodeAndTrackNo",
            data: {
                sppId: sppId,
                odaId: odaId
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.confirmPost({
                        content: result.msg,
                        url: ctx + "buy/outsourceReturn/submit",
                        data: {
                            sppId: sppId,
                            odaId: odaId
                        },
                        success: function (result) {
                            if (result.type === web_status.SUCCESS) {
                                js.modal.success(result.msg);
                                js.table.refresh(table);
                                js.modal.hide("modal-confirm-return");
                                js.modal.hide("modal-select");
                            } else {
                                js.modal.warning(result.msg);
                            }
                        }
                    });

                } else {
                    js.confirmPost({
                        content: "确认回货吗？",
                        url: ctx + "buy/outsourceReturn/submit",
                        data: {
                            sppId: sppId,
                            odaId: odaId,
                            brand: $("#confirmReturnForm [name='brand']").val().trim()
                        },
                        success: function (result) {
                            if (result.type === web_status.SUCCESS) {
                                js.modal.success(result.msg);
                                js.table.refresh(table);
                                js.modal.hide("modal-confirm-return");
                                js.modal.hide("modal-select");
                            } else {
                                js.modal.warning(result.msg);
                            }
                        }
                    });
                }
                js.table.refresh(table);
                js.modal.hide("modal-select");
            }
        })

        // js.post({
        //     url: ctx + "buy/outerReturn/checkIsHaveBrand",
        //     data: {
        //         sppId: sppId
        //     },
        //     success: function (result) {
        //         $("#btn-confirm-return-submit").attr("data",result.data);
        //         $("#btn-confirm-return-submit").attr("data-sppId",sppId);
        //         $("#btn-confirm-return-submit").attr("data-odaId",odaId);
        //         $("#confirmReturnForm")[0].reset()
        //         js.modal.open("modal-confirm-return");
        //     },
        //     error: function () {
        //         js.modal.fail();
        //     }
        // })


        // js.confirmPost({
        //     content: "确认回货吗？",
        //     url: ctx + "buy/outerReturn/submit",
        //     data: {
        //         sppId: sppId,
        //         odaId: odaId
        //     },
        //     success: function (result) {
        //         if (result.type === web_status.SUCCESS) {
        //             js.modal.success(result.msg);
        //             js.table.refresh(table);
        //             js.modal.hide("modal-select");
        //         } else {
        //             js.modal.warning(result.msg);
        //         }
        //     }
        // });
    }

    /** 批量确认回货 */
    function batchOrderConfirmation() {
        var arr = js.table.selectColumns(table, "sppId");
        if (arr.length < 1) {
            js.modal.alert("请选择要确认回货的采购计划");
            return;
        }
        js.post({
            url: ctx + "buy/outsourceReturn/submit/all",
            data: {
                sppId: js.table.selectColumns(table, "sppId"),
                odaId: js.table.selectColumns(table, "odaId")
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        })
    }

    /** 撤消采购计划回货确认 */
    function cancel(id) {
        js.confirmPost({
            content: "删除数据将无法恢复，确认删除吗？",
            url: ctx + "buy/outsourceReturn/cancel",
            data: {
                id: id
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }

    /** 预入库 */
    function pre(sppId) {
        js.confirmPost({
            content: "确认预入库吗？",
            url: ctx + "buy/outerReturn/pre",
            data: {
                id: sppId
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(preTable);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }

    /** 批量预入库 */
    function preAll() {
        var arr = js.table.selectColumns(preTable, "sppId");
        if (arr.length < 1) {
            js.modal.alert("请选择要预入库的采购计划");
            return;
        }
        js.post({
            url: ctx + "buy/outerReturn/pre/all",
            data: {
                ids: arr
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(preTable);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        })
    }

    /** 撤消预入库 */
    function cancelPre(id) {
        js.confirmPost({
            content: "是否撤消预入库？",
            url: ctx + "buy/outerReturn/cancel/pre",
            data: {
                id: id
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(preTable);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }

    $(function () {
        $("title").html($("title").html() + " —— 外协件回货确认");
        /** 初始化表格 */
        table = js.table.init({
            url: ctx + "buy/outsourceReturn/list",
            toolbar: '#toolbar-main',
            showExport: false,
            pageSize: 300,                                       // 每页的记录行数
            pageList: [10, 15, 25, 50, 100, 300, 500, 1000],               // 可供选择的每页的行数
            showColumns: false,
            columns: [
                {
                    checkbox: true,
                    width: '30px',
                    formatter: function (value, row) {
                        if (sessionUser.name === row.sppPlanner && row.odaId && !row.sppOdaId) {
                            return {disabled: false}
                        } else {
                            return {disabled: true}
                        }
                    }
                },
                {
                    title: '序号', width: '50px',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: "计划员", field: 'sppPlanner'},
                {title: "SAP<br>物料编码", field: 'sppSapCode'},
                {title: "采购计划<br>物料描述", field: 'sppMatDesc'},

                {
                    title: "采购计划<br>数量/单位", field: 'sppNum',
                    formatter: function (value, row, index) {
                        if (row.sppNum == null) {
                            row.sppNum = '';
                        }
                        return row.sppNum + row.sppUnit;
                    }
                },
                {title: "提报计划<br>图号", field: 'rppDiagram'},
                {title: "采购计划<br>跟踪号", field: 'sppTrackCode'},
                {title: "采购计划<br>项目编号", field: 'sppProjectCode'},
                {title: "采购计划<br>项目名称", field: 'sppProjectName'},

                {
                    title: "采购计划<br>确认状态", field: 'sppRetStatus',
                    formatter: function (value, row, index, field) {
                        if (value) {
                            return '已确认';
                        }
                        return '未确认';
                    }
                },
                {title: "入库记录<br>物料描述", field: 'odaMatName'},
                {
                    title: "入库记录<br>数量", field: 'odaNum'
                },
                {title: "入库记录<br>图号", field: 'odaDrawingCode'},
                {title: "入库记录<br>计划跟踪号", field: 'odaTrackCode'},
                {title: "入库记录<br>使用周期", field: 'odaProCode'},
                {title: "入库记录<br>项目文本", field: 'odaProName'},

                // { title: "入库记录<br>记账日期", field: 'odaBookkeepingDate'},
                {
                    title: "操作",
                    width: '60px',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.odaId) {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="openInfoModal(' + js.jsonStringify(row) + ')"> 查看</a> ');
                        }
                        if (sessionUser.name === row.sppPlanner) {
                            if (row.odaId && !row.sppOdaId && hasP("plan:outsourceretconfirm:confirm")) {
                                actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="submitReturn(' + row.sppId + ',' + row.odaId + ')"> 确认</a> ');
                            }
                            if (row.sppOdaId && hasP("plan:buyretconfirm:confirm")) {
                                actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="cancel(' + row.sppId + ')"> 撤消</a> ');
                            }
                            if (!row.sppOdaId && hasP("plan:buyretconfirm:confirm")) {
                                actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openSelectModal(' + js.jsonStringify(row) + ')"> 选择</a> ');
                            }
                        }
                        return actions.join('');
                    }
                }
            ]
        });
        /** 表格搜索 */
        $("#btn-search").click(function () {
            js.table.search(table);
        });


        /** 原数据表格 */
        srcTable = js.table.init({
            id: "src-table",
            showExport: false,
            showColumns: false,
            url: false,
            pagination: false,
            sidePagination: null,
            firstLoad: false,
            columns: [
                {title: "采购计划<br>物料描述", field: 'sppMatDesc'},
                {title: "SAP<br>物料编码", field: 'sppSapCode'},
                {title: "采购计划<br>数量", field: 'sppNum'},
                {title: "提报计划<br>图号", field: 'rppDiagram'},
                {title: "采购计划<br>项目编号", field: 'sppProjectCode'},
                {title: "采购计划<br>项目名称", field: 'sppProjectName'},
                {title: "采购计划<br>跟踪号", field: 'sppTrackCode'},
                {title: "采购计划<br>回货状态", field: 'sppRetStatus'}
            ]
        });
        /** 入库记录表格 */
        storeTable = js.table.init({
            id: "store-table",
            queryForm: "storeForm",
            url: ctx + "buy/outsourceReturn/store/list",
            showExport: false,
            showColumns: false,
            firstLoad: false,
            columns: [
                {
                    title: '序号', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {
                    title: "物料描述", field: 'odaMatName', width: '100',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 90px">' + value + '</p>';
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "数量", field: 'odaNum', width: '100',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 90px">' + value + '</p>';
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "单位", field: 'odaUnit', width: '100',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 90px">' + value + '</p>';
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "SAP物料编码", field: 'odaSapCode', width: '100',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 90px">' + value + '</p>';
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "图号", field: 'odaDrawingCode', width: '100',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 90px">' + value + '</p>';
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "计划跟踪号", field: 'odaTrackCode', width: '100',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 90px">' + value + '</p>';
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "项目文本", field: 'odaProName', width: '100',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 90px">' + value + '</p>';
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "使用周期", field: 'odaProCode', width: '100',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 90px">' + value + '</p>';
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "记账日期", field: 'odaBookkeepingDate', width: '100',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 90px">' + value + '</p>';
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "操作",
                    width: '60px',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="submitReturn(' + shoppingPlanId + ',' + row.odaId + ')"> 确认</a> ');
                        return actions.join('');
                    }
                }
            ]
        });
        /** 入库记录表格搜索 */
        $("#btn-search-store").click(function () {
            js.table.search(storeTable);
        });
        /** 选择入库记录模态框隐藏事件，重置查询表单*/
        js.modal.hideEvent("modal-select", function () {
            js.reset("storeForm");
        });


    });

</script>
</body>
</html>
